// 引入SDK核心类，js文件根据自己业务，位置可自行放置
var QQMapWX = require('./qqmap-wx-jssdk');
// 实例化API核心类
var qqmapsdk ;
Page({
  data: {
    userId:null,
    community: {
      leader:{
        name : "暂无"
      }
    },
    currentLocation: '',
    nearbyCommunities: [
      {
        memberCount: 1,
        leader: "王卓煜"
      }
    ],
    location:{
      latitude: 38.814709,
      longitude: 115.485083
    }
  },
  getLocationInfo(){
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: 'RJ6BZ-SHDKW-OCDR3-YRCRE-CL2VQ-Z4BST'
    });
    const latitude = this.data.location.latitude
    const longitude = this.data.location.longitude
    const location = {
      latitude: this.data.location.latitude,
      longitude: this.data.location.longitude
    };
    qqmapsdk.search({
      keyword: '社区',
      location: location,
      success: function (res) {
        wx.request({
          url: `http://localhost:9301/community/findCommunityInfo`,
          method:"POST",
          data:{
            nearbyCommunities : res.data
          },
          success: (res)=>{
            this.setData({
              nearbyCommunities: res.data.data
            })
            console.log("查询社区",res.data);
          }
        })
      }.bind(this),
      fail: function (err) {
        console.log('请求失败', err);
      },
    })
    
  },
  onLoad(options) {
    wx.getStorage({
      key: "userInfo",
      success: function (res) {
        console.log("全局存储", res.data);
        this.handleFindCommunity(res.data.id)
        if (res.data) {
          this.setData({
            userId: res.data.id
          });
        }
      }.bind(this)
    }),
    this.getLocationInfo()
  },
  // 查询当前用户的社区
  handleFindCommunity(id){
    console.log("查询当前用户的社区参数:",id);
    wx.request({
      url: `http://localhost:9301/community/findByUserId?id=${id}`,
      method:"GET",
      success: (res)=>{
        console.log("查询当前用户的社区",res);
        this.setData({
          community: res.data.data,
          currentLocation: res.data.data.address
        })
      }
    })
  },
  getLocation() {
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        wx.chooseLocation({
          success: (location) => {
            console.log("chooseLocation的信息", location);
            this.setData({
              currentLocation: location.address,
              "location.latitude": location.latitude,
              "location.longitude": location.longitude,
            }, () => {
              // 位置更新后，重新获取周边社区信息
              this.getLocationInfo();
            });
            console.log(this.data.currentLocation);
            console.log("data:location", this.data.location);
          }
        });
      },
      fail: (err) => {
        console.error('获取位置失败', err);
        wx.showToast({
          title: '获取位置失败',
          icon: 'none',
          duration: 2000
        });
      }
    })
  },
  // 切换社区
  handleChangeCommunity(e){
    const title = e.currentTarget.dataset.title
    const address = e.currentTarget.dataset.address
    const lat = e.currentTarget.dataset.lat
    const lng = e.currentTarget.dataset.lng
    const qqMapId = e.currentTarget.dataset.id
    console.log(qqMapId);
    wx.request({
      url: `http://localhost:9301/community/changeUserCommunity`,
      method:"POST",
      data:{
        userId: this.data.userId,
        qqMapId: qqMapId,
        title: title,
        address: address,
        lat: lat,
        lng: lng,
      },
      success: (res)=>{
        wx.showToast({
          title: '切换拼团点成功',
        })
        setTimeout(()=>{
          wx.switchTab({
            url: '/pages/index/index',
          })
        },1000)
      }
    })
  },
  // 添加拼团点
  handleAddCommunity(list){
    wx.request({
      url: 'http://localhost:9301/community/batchAddCommunity',
      method:"POST",
      data:{
        location: this.data.location,
        nearbyCommunities : list
      },
    })
  },
  onPullDownRefresh() {
    console.log('触发下拉刷新');
    // 模拟刷新数据
    this.onLoad()
    console.log("刷新了");
  },
});